﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CYTS.Aoyou.Order;
using CYTS.Aoyou.Framework.Database;
using CYTS.Aoyou.Order.DAInterface;
using CYTS.Aoyou.Framework.Log;
using CYTS.Aoyou.Order.ServiceInterface;
using CYTS.Aoyou.Framework.Service;
using CYTS.Aoyou.Order.CAInterface;
using CYTS.Aoyou.Framework.Cache;
using CYTS.Aoyou.Framework.Core;
using System.ServiceModel;

namespace CYTS.Aoyou.Order.Bussiness
{
    public class OrderService : IOrderService, IService
    {
        public ServiceEntity.OrderInfo GetOrderInfo(int orderID)
        {
            //AopTest at = new AopTest();
            //at.DO("aaaa", 1, 2, new List<string> { "abc", "abc" });
            ServiceEntity.OrderInfo returnOrderInfo = null;
            Entity.OrderInfo orderInfo = null;
            try
            {
                IOrderInfoDataAdapter dataAdapter = DataAdapterFactory.GetInstance().GetDataAdapter<IOrderInfoDataAdapter>();
                orderInfo = dataAdapter.GetOrderInfo(orderID);
            }
            catch (Exception ex)
            {
                CommonLogger.LogError("查询订单信息报错", ex);
            }

            if (orderInfo != null)
            {
                returnOrderInfo = new ServiceEntity.OrderInfo();
                returnOrderInfo.OrderID = orderInfo.OrderID;
                returnOrderInfo.OrderMoney = orderInfo.OrderMoney;
                returnOrderInfo.PaidMoney = orderInfo.PaidMoney;
                returnOrderInfo.UnpaidMoney = orderInfo.OrderMoney - orderInfo.PaidMoney;
                returnOrderInfo.ProductName = orderInfo.ProductName;
            }
            return returnOrderInfo;
        }


        public ServiceEntity.OrderInfo GetOrderInfoFromCache(int orderID)
        {
            ServiceEntity.OrderInfo returnOrderInfo = null;
            Entity.OrderInfo orderInfo = null;
            try
            {
                IOrderInfoDataAdapter dataAdapter = DataAdapterFactory.GetInstance().GetDataAdapter<IOrderInfoDataAdapter>();
                orderInfo = dataAdapter.GetOrderInfo(orderID);
            }
            catch (Exception ex)
            {
                CommonLogger.LogError("从数据库查询订单信息报错", ex);
            }

            if (orderInfo != null)
            {
                IOrderInfoCacheAdapter cacheAdapter = null;
                try
                {
                    cacheAdapter = CacheAdapterFactory.GetInstance().GetCacheAdapter<IOrderInfoCacheAdapter>();
                    cacheAdapter.PutOrderInfo(orderInfo);
                }
                catch (Exception ex)
                {
                    CommonLogger.LogError("将订单信息写入缓存报错", ex);
                }
                //System.Threading.Thread.Sleep(1000 * 5);
                orderInfo = null;

                try
                {
                    cacheAdapter = CacheAdapterFactory.GetInstance().GetCacheAdapter<IOrderInfoCacheAdapter>();
                    orderInfo = cacheAdapter.GetOrderInfo(orderID);
                }
                catch (Exception ex)
                {
                    CommonLogger.LogError("从缓存获取订单信息报错", ex);
                }
            }

            

            if (orderInfo != null)
            {
                returnOrderInfo = new ServiceEntity.OrderInfo();
                returnOrderInfo.OrderID = orderInfo.OrderID;
                returnOrderInfo.OrderMoney = orderInfo.OrderMoney;
                returnOrderInfo.PaidMoney = orderInfo.PaidMoney;
                returnOrderInfo.UnpaidMoney = orderInfo.OrderMoney - orderInfo.PaidMoney;
                returnOrderInfo.ProductName = orderInfo.ProductName;
            }

            return returnOrderInfo;
        }
    }

    [AOP]
    public class AopTest : ContextBoundObject
    {
        [AOPMethod(typeof(AOPLogHandler))]
        public void DO(string id, int a, decimal b, List<string> list)
        {
            try
            {
                throw new Exception("error");
            }
            catch { }
        }
    }
}
